package com.bupt.godxian.charging.mapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.bupt.godxian.charging.entity.ChargeRequest;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ChargeRequestMapper extends BaseMapper<ChargeRequest> {
    @Insert("INSERT INTO charge_request(state, user_id, charge_mode, require_amount, charge_time, battery_size, charge_id, request_submit_time) " +
            "VALUES(#{state}, #{userId}, #{chargeMode}, #{requireAmount}, #{chargeTime}, #{batterySize}, #{chargeId}, #{requestSubmitTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(ChargeRequest chargeRequest);


    @Select("SELECT * FROM charge_request WHERE user_id = #{userId} AND state != 0 LIMIT 1")
    ChargeRequest getUndoChargeRequestByUserId(@Param("userId") Long userId);

    @Select("SELECT * FROM charge_request WHERE id = #{requestId} AND state != 0 LIMIT 1")
    ChargeRequest selectByIdone(@Param("requestId") Long requestId);

    // charge_mode = session.query(ChargeRequest.charge_mode).filter(
    //            ChargeRequest.id == request_id).first()[0]
    @Select("SELECT * FROM charge_request WHERE user_id = #{userId} LIMIT 1")
    ChargeRequest getChargeRequestByUserId(@Param("userId") Long userId);

    @Select("SELECT * FROM charge_request WHERE id = #{requestId} LIMIT 1")
    ChargeRequest getChargeRequestById(@Param("requestId") Long requestId);

    @Select("SELECT * FROM charge_request WHERE id = #{id}")
    ChargeRequest selectByPrimaryKey(Long id);

    // 添加其他需要的数据库操作方法
    @Select("SELECT COUNT(*) FROM charge_request WHERE charge_mode = #{chargeMode} AND state != 0")
    int getHisFrontCarsCount(@Param("chargeMode") String chargeMode);

    @Select("SELECT * FROM charge_request WHERE charge_mode = #{chargeMode} AND state != 0")
    List<ChargeRequest> findByChargeModeAndStateNotZero(@Param("chargeMode") String chargeMode);

    @Update("UPDATE charge_request SET state = #{state} WHERE id = #{id}")
    void updateState(@Param("id") Long id, @Param("state") Integer state);

    @Update("UPDATE charge_request SET charge_pile_id = #{chargePileId} WHERE id = #{id}")
    void updateChargePileId(@Param("id") Long id, @Param("chargePileId") Long chargePileId);

    @Select("SELECT *" +
            "FROM charge_request cr " +
            "JOIN user u ON cr.user_id = u.id " +
            "WHERE cr.state != 0")
    List<ChargeRequest> getUserInQueue();

}

